package cn.tedu.mvc.controller;

import cn.tedu.mvc.pojo.dto.RequestDto;
import cn.tedu.mvc.pojo.vo.JsonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@Validated
@RestController//==@Controller+@ResponseBody
public class RequestController {
    /*
     * GET http://localhost:8080/api/param?value=100
     * 这里的value参数名是value，所以这里方法的参数名也是value，才可以接收参数值
     */


    @GetMapping("/api/param1")
    public String handleRequest( @NotBlank(message = "参数值不能为空") String value) {
         return value;
    };

    /**
     * GET http://localhost:8080/api/param?paramId=10&name=Tom
     * @param id
     * @param name
     * @return
     */

    @GetMapping("/api/param2")
    public String handleRequest(@RequestParam(
                                 name = "paramId",
                                 required = false,
                                 defaultValue = "20") String id,
                                @RequestParam String name) {
        return id+"/"+name;
    }
    @GetMapping("/api/param3/{id}/{name}")
    public String handleRequest(@PathVariable("id") Long id,
                                @PathVariable("name") String name) {
        return id+"/"+name;
    }
    @PostMapping("/api/param4")
    public RequestDto handleRequest(@RequestBody @Validated RequestDto requestDto) {
        return requestDto;
    }
    @PostMapping("/api/resp")
    public JsonResult handleRequestBody(@RequestBody RequestDto requestDto) {
        return new JsonResult(requestDto);
    }
}
